import request from "./request"

// 获取商品列表
export const getProducts = async (page: number = 1, pageSize: number = 10) => {
    try {
        const data = await request.get(`/api/products?page=${page}&pageSize=${pageSize}`)
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取商品数据失败:', error)
        return { 
            data: {
                list: [],
                pagination: {
                    page: 1,
                    pageSize: 10,
                    total: 0,
                    totalPage: 0
                }
            },
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 获取商品详情
export const getProductById = async (id: number) => {
    try {
        const data = await request.get(`/api/products/${id}`)
        return { 
            data,
            message: '获取数据成功',
            code: 200
        }
    } catch (error: any) {
        console.error('获取商品详情失败:', error)
        return { 
            data: null,
            message: error.message || '获取数据失败',
            code: 500
        }
    }
}

// 创建商品
export const createProduct = async (productData: any) => {
    try {
        const data = await request.post('/api/products', productData)
        return { 
            data,
            message: '创建商品成功',
            code: 200
        }
    } catch (error: any) {
        console.error('创建商品失败:', error)
        return { 
            data: null,
            message: error.message || '创建商品失败',
            code: 500
        }
    }
}

// 更新商品
export const updateProduct = async (id: number, productData: any) => {
    try {
        const data = await request.put(`/api/products/${id}`, productData)
        return { 
            data,
            message: '更新商品成功',
            code: 200
        }
    } catch (error: any) {
        console.error('更新商品失败:', error)
        return { 
            data: null,
            message: error.message || '更新商品失败',
            code: 500
        }
    }
}

// 删除商品
export const deleteProduct = async (id: number) => {
    try {
        const data = await request.delete(`/api/products/${id}`)
        return { 
            data,
            message: '删除商品成功',
            code: 200
        }
    } catch (error: any) {
        console.error('删除商品失败:', error)
        return { 
            data: null,
            message: error.message || '删除商品失败',
            code: 500
        }
    }
}